<?php

namespace app\comm\service;
use app\mall\model\Store;
use app\comm\model\StoreGoods;
use think\Db;

/**
 * 商品，门店，各种计算列表
 */
class SrotGoodServices{

	/**
	 * 列表查询
	 * @return [type] [description]
	 */
	public function storlist($lng,$lat,$city){
		$xyz  = $this->getXyPoint($lng,$lat);
				$map['store.lng'] = array('between',array($xyz['lt']['lng'],$xyz['rb']['lng']));
				$map['store.lat'] = array('between',array($xyz['rb']['lat'],$xyz['lt']['lat']));
		$field="(((store.lng-$lng)*(store.lng-$lng)+(store.lat-$lat)*(store.lat-$lat)))*100000 as y , ";
		$field.='id,title,lng,lat,address,logo,images,description,detail,addtime';
		$store = Store::alias('store')->where($map);
		if(!empty($city)){
			$store=$store->where("city_key",$city);
		}
		$store = $store ->field($field)->order('y asc')->select();
		$pusharay=[];
		foreach ($store as $key => $value) {
			$ordr = $value->toArray();
			$ordr['juli'] = $this->getdispoint($lat,$lng,$ordr['lat'],$ordr['lng'],0);
			array_push($pusharay, $ordr);
		}
		return $pusharay;
	}

	/**
	 * 计算经纬度 返回经纬度的计算结果，是一个between and 形式的json
	 */
	public	function getXyPoint($lng, $lat,$distance = 10000){
		$earth_radius = 6378.138;
		$dlng =  2 * asin(sin($distance / (2 * $earth_radius)) / cos(deg2rad($lat)));
		$dlng = rad2deg($dlng);
		$dlat = $distance/$earth_radius;
		$dlat = rad2deg($dlat);
		return array(
			'lt'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
			'rt'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
			'lb'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
			'rb'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
		);
	}

	/**
	 * 	计算距离
	 * @param  [type] 当前精度
	 * @param  [type] 当前维度
	 * @param  [type] 门店精度
	 * @param  [type] 门店维度
	 * @param  [type] 选择的计算方式
	 * @return [type] 返回距离
     * 35.95166   120.15944  35.958496749206  120.21075979183
     */
	public function  getdispoint($lat1, $lng1, $lat2, $lng2,$app){
			$earthRadius = 6367000; //approximate radius of earth in meters
			$lat1 = ($lat1 * pi() ) / 180;
			$lng1 = ($lng1 * pi() ) / 180;

			$lat2 = ($lat2 * pi() ) / 180;
			$lng2 = ($lng2 * pi() ) / 180;

			$calcLongitude = $lng2 - $lng1;
			$calcLatitude = $lat2 - $lat1;
			$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
			$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
			if($app==1){
				 $calculatedDistance = $earthRadius * $stepTwo;
				 return round($calculatedDistance)/1000;
			}
			if($app==2){
				$calculatedDistance = $earthRadius * $stepTwo/1000;
				return round($calculatedDistance,2)/1000;
			}
    }

	/**
	 * 获取到商店的商品
	 * @return [type] [description]
	 */
	public function getstorgoods($id,$page){
		$list = StoreGoods::alias('a')->join($this->_join())
							->where('1=1')->where($this->_map(['storeid'=>$id]))->field($this->_fields())->page($page,20)->select();
		$psuh = [];
		foreach ($list as $key => $value) {
			$val= $value->toArray();
			array_push($psuh, $val);
		}
		return $psuh;
	}

	/**
	 * 获得到商店的商品serch
	 */
	public function serch($msg,$id,$page,$catge){
		 $map["store_id"]=["eq",$id];
		if($catge=="0" && $msg!=''){
			$map["name"]=["like","%".$msg."%"];
		}
		if($catge!="0"){
			$map["cat_id"]=["eq",$catge];
		}
		// $map["status"]=["eq",1];
		// $map["is_deleted"]=["eq",0];
		//return $map;
		$resutl = Db::table("ys_view_storgood")->where($map)->page($page)->select();
		foreach ($resutl as $key => &$value) {
			$value["detail"]=htmlspecialchars_decode($value["detail"]);
		}
		return $resutl;
	}

	/**
	 * 商店商品
	 * @return [type] [description]
	 */
	public function getcatagstorgoods(){
		$resutl = Db::table("ys_goods_category")->select();
		return $resutl;
	}
	/**
	 * 
	 * 门店商品字段查询，连接查询
	 */
	protected function _fields($type=1){
		$fields ="";
		$fields = 'b.*,a.id as sid ';
		return $fields ;
	}
	
	/**
	 *字段映射
	 * @return [type] [description]
	 */
	protected function _map($array=[]){
		$map['a.is_deleted']   =  ['eq',0];
		$map['a.onsale']       =  ['eq',1];
		$map['a.store_id']     =  ['eq',$array['storeid']];
		return $map;
	} 
	/**
	 * 关联查询
	 */
	protected function _join($json=[]){
		$join = [
		    ['ys_goods b','a.goods_id=b.id']
		];
		return $join;
	}

}